/**
 * 
 */
package com.cnhaimei.vod.net;

import android.R.integer;

import com.cnhaimei.vod.SelSongsManager;
import com.cnhaimei.vod.bean.Song;
import com.cnhaimei.vod.logic.EventsManager;
import com.cnhaimei.vod.logic.EventsManager.EventsListener;
import com.cnhaimei.vod.net.dq.DQRequest;

/**
 * 所有网络与逻辑操作间的对接类
 * 
 * @author Administrator
 * 
 */
public class XVod {
	private static final String TAG = "XVod";

	private static XVod global;
	private static Object lock = new Object();

	private byte[] mDataBuf = new byte[1024];
	private byte[] mMImgBuf = new byte[3 * 1024]; // ??

	// private DCManager dcManager;
	// private DQManager dqManager;
	// private DCHeart dcHeart;
	private String netAddr;

	public static XVod G() {
		if (global == null) {
			synchronized (lock) {
				if (global == null) {
					synchronized (lock) {
						XVod vod = new XVod();
						global = vod;
					}
				}
			}
		}
		return global;
	}

	public void registerListener(int event, EventsListener listener) {
		EventsManager.G().registerListener(event, listener);
	}

	public void unRegisterListener(int event, EventsListener listener) {
		EventsManager.G().unRegisterListener(event, listener);
	}

	public void downloadRooms() {
		NetManager.G().dcHeart.requestRoomList();
	}

	//单机版请求注册
	public void requestRegRooms() {
		NetManager.G().dcHeart.requestRegRooms();
	}
	
	public void requestRoomInfo(String room) {
		NetManager.G().dcHeart.requestRoomIp(room);
	}

	public void passwordCheck(String password) {
		NetManager.G().dcManager.sendOperator(DCManager.PASSWORD_CHECK, password);
	}
	
	public long addRequest(DQRequest req) {
		return NetManager.G().dqManager.addRequest(req);
	}

	/** 查询歌曲 */
//	public long searchSong() {
		// dqManager.requestQuery("song_valid",
		// "NAME_LN >= 2 && NAME_PY >= \"AA\"");
//		return NetManager.G().dqManager.requestQuery("song_valid", "NAME_LN >= 0");
//		return 0;
//	}

	/** 查询歌星 */
//	public long searchSinger() {
		//return NetManager.G().dqManager.requestQuery("star_valid", "NAME_LN >= 0");
//		return 0;
//	}
	
	/** 查询某一歌星的所有歌曲*/
//	public long searSingerSong(String singer) {
//		return NetManager.G().dqManager.requestQuery("song_valid", "STAR == \"|" + singer + "|\"");
//		return 0;
//	}

	//public long search(String tableName, String query) {
	//	Log.e(TAG, "request search tableName: " + tableName + " dquery:" + query);
		//return NetManager.G().dqManager.requestQuery(tableName, query);
	//	return 0;
	//}

//	/**
//	 * 对snap快照从from读取size条歌曲记录
//	 * 
//	 * @param snap
//	 * @param from
//	 * @param size
//	 */
//	public long requestRecord(Parser paser, int from, int size) {
//		Log.e(TAG, "request record from:" + from + " size: " + size);
//		return NetManager.G().dqManager.requestData(paser, from, size);
//		return 0;
//	}

//	public void closeQuery(byte snap) {
//		Log.e(TAG, "close search snap:" + snap);
//		NetManager.G().dqManager.requestClose(snap);
//	}
	
	public void startKeepPlayerHeart() {
		NetManager.G().dcManager.starAutoHeart();
	}

	public void setPlayerState(int state) {
		NetManager.G().dcManager.sendOperator(state, 0);
	}

	public void setPlayerSet(int set, int value) {
		NetManager.G().dcManager.sendOperator(set, value);
	}
	
	public void callServer(int value) {
		NetManager.G().dcManager.sendOperator(DCManager.CTRL_SERVER, value);
	}
	
	public void callServer(String value) {
		NetManager.G().dcManager.sendOperator(DCManager.CTRL_SERVER, value);
	}

	public void deleteSong(int no) {
		NetManager.G().dcManager.sendOperator(DCManager.CTRL_DELETE_SONG, no);
	}
	
	public void insertSong(Song song) {
		NetManager.G().dcManager.insertSong(song);
		SelSongsManager.G().addSong(song);
	}

	public void preSortSong(int pos, int moveTo) {
		NetManager.G().dcManager.sendOperator(DCManager.CTRL_MOVETO_SONG, pos,
				moveTo);
	}
	
	public void requestSelSong(int from, int count) {
		NetManager.G().dcManager.requestSelSong(from, count);
	}
	
	public void requestDoneSong(int from ,int count) {
		NetManager.G().dcManager.requestSingedSong(from, count);
	}

	public void close() {
		global = null;
	}

}
